package com.wandian.news.mvvm.lifetool.nl;

import android.content.Context;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.TimeUtils;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.widget.datetimepicker.DateTimePicker;
import com.wandian.news.R;
import com.wandian.news.databinding.NewsActivityLifeConstellationBinding;
import com.wandian.news.databinding.NewsActivityLifeNlBinding;

import java.util.Date;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 农历activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NEWS_NL)
public class LifeNLActivity extends BaseMVVMActivity<LifeNLActivityViewModel>   {
    private NewsActivityLifeNlBinding dataBinding;
    private Date chooseData = null;

    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.news_activity_life_nl);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        chooseData = new Date();
        initViewData();
        initClink();
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("黄历查询");
        dataBinding.tvChooseData.setText(TimeUtils.getDateTime(chooseData)+"");
        //获取今日数据
        getDataNL(chooseData);
        mViewModel.dataBean.observe(this,dataBean->{
            if(!UIUtils.isEmpty(dataBean)){
                dataBinding.setBean(dataBean);
            }
        });
    }


    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        dataBinding.tvChooseData.setOnClickListener(v -> showYearDateDialog(this));
        dataBinding.rlData.setOnClickListener(v -> showYearDateDialog(this));
    }

    //日期选择器
    private  void showYearDateDialog(Context context) {
        DateTimePicker dateTimePicker = new DateTimePicker.Builder(context)
                .setCancelTextColor(context.getResources().getColor(com.mylib.libcore.R.color.text_black))
                .setOkTextColor(context.getResources().getColor(com.mylib.libcore.R.color.button_blue_normal))
                .setTitleTextColor(context.getResources().getColor(com.mylib.libcore.R.color.text_gray))
                .setTextColor(context.getResources().getColor(com.mylib.libcore.R.color.text_gray))
                .setSelectedTextColor(context.getResources().getColor(com.mylib.libcore.R.color.text_black))
                .setDividerColor(context.getResources().getColor(com.mylib.libcore.R.color.line_color))
                .setDividerWidth(2f)
                .setKeepLastSelected(true)
                .setShowYMDHMLabel(false)
                .setTextSize(context.getResources().getDimensionPixelSize(com.mylib.libcore.R.dimen.text_30px), context.getResources().getDimensionPixelSize(com.mylib.libcore.R.dimen.text_30px))
                .setShowType(DateTimePicker.ShowType.DAY)
                .build();
        dateTimePicker.setResultHandler(date -> getDataNL(date));
        dateTimePicker.show(new Date());
    }

    private void getDataNL(Date dateSelect){
        this.chooseData = dateSelect;
        if(chooseData != null){
            dataBinding.tvChooseData.setText(TimeUtils.getDateTime(chooseData)+"");
            mViewModel.getData(TimeUtils.getDateTime(chooseData)+"");
        }else {
            TimeUtils.stampToDate("请选择日期");
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
